/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ueb13;

import java.io.IOException;
import ueb13.htw.io.StringLineReader;
import ueb13.uebOrder.Dialog;
import ueb13.uebOrder.PriorityOrderManagment;

/**
 *
 * @author steffenschuhmann
 */
public class PriorityQueueDialog extends Dialog {

    private PriorityOrderManagment manager = new PriorityOrderManagment();
    private static final char ADD       = '+';
    private static final char REMOVE    = '-';
    private static final char CHANGEP   = 'c';
    
    @Override
    public void execute(String command) {
        String line = null;
        int counter = 0;
        boolean theEndIsNear = false;
        StringLineReader reader = null;

        try {
            reader = new StringLineReader(command);

            while (!theEndIsNear) {
                line = reader.read(counter);
                if (line == null)
                    throw new NullPointerException();
                pushToQueue(line, counter);
                counter++;
            }
        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (NullPointerException n) {
            n.getMessage();
        }
        System.out.println("Queue:");
        System.out.println(manager.toString());
    }
    
    private void pushToQueue(String command, int lineNumber) {
        String[] line = null;
        command.trim();
        if(command.charAt(0) == REMOVE) {
            System.out.println(lineNumber + manager.remove().toString() + " removed!");
        } else if(command.charAt(0) == ADD) {
            line = command.split("\\+");
            line = line[1].split("\\t");
            try {
                System.out.println(lineNumber + manager.add(new Integer(line[0]), line[1]).toString() 
                        + " added!");
                
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else if (command.charAt(0) == CHANGEP){
            
        } else {
            System.out.println("Wrong Statement:" + line);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PriorityQueueDialog dialog = new PriorityQueueDialog();
        dialog.setUp(dialog);
    }
}
